package com.xbts.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.system.domain.*;
import com.xbts.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.config.XbtsConfig;
import com.xbts.common.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页
 *
 * @author xbts
 */
@RestController
public class SysIndexController
{
    /** 系统基础配置 */
    @Autowired
    private XbtsConfig xbtsConfig;
    @Autowired
    private ICnNewsService cnNewsService;
    @Autowired
    private ICnCloudMarketService cnCloudMarketService;
    @Autowired
    private ICnGovernmentCloudService cnGovernmentCloudService;
    @Autowired
    private ICnHelpService cnHelpService;
    @Autowired
    private ICnSolutionService cnSolutionService;

    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index()
    {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", xbtsConfig.getName(), xbtsConfig.getVersion());
    }


    /**
     * 首页全局搜索
     *
     */
    @GetMapping("/index/global/search")
    public AjaxResult globalSearch(String keyword)
    {
        ArrayList<Map<String, String>> resultList = new ArrayList<>();
        // 新闻
        List<CnNews> cnNewsList = cnNewsService.list(new LambdaQueryWrapper<CnNews>()
                .like(CnNews::getNewsTittle, keyword));
        for (CnNews cnNews : cnNewsList) {
            Map<String, String> cnNewsMap = new HashMap<>();
            cnNewsMap.put("type", "news");
            cnNewsMap.put("id", cnNews.getNewsId().toString());
            cnNewsMap.put("tittle", cnNews.getNewsTittle());
            resultList.add(cnNewsMap);
        }

        // 云市场
        List<CnCloudMarket> cnCloudMarketList = cnCloudMarketService.list(new LambdaQueryWrapper<CnCloudMarket>()
                .like(CnCloudMarket::getTitle, keyword)
                .eq(CnCloudMarket::getDeleteFlag, false));
        for (CnCloudMarket cnCloudMarket : cnCloudMarketList) {
            Map<String, String> cnCloudMarketMap = new HashMap<>();
            cnCloudMarketMap.put("type", "cloudMarket");
            cnCloudMarketMap.put("id", cnCloudMarket.getMarketId().toString());
            cnCloudMarketMap.put("tittle", cnCloudMarket.getTitle());
            resultList.add(cnCloudMarketMap);
        }


        // 政务云
        List<CnGovernmentCloud> cnGovernmentCloudList = cnGovernmentCloudService.list(new LambdaQueryWrapper<CnGovernmentCloud>()
                .like(CnGovernmentCloud::getServerName, keyword)
                .eq(CnGovernmentCloud::getDeleteFlag, false));
        for (CnGovernmentCloud cnGovernmentCloud : cnGovernmentCloudList) {
            Map<String, String> cnGovernmentCloudMap = new HashMap<>();
            cnGovernmentCloudMap.put("type", "governmentCloud");
            cnGovernmentCloudMap.put("id", cnGovernmentCloud.getGovId());
            cnGovernmentCloudMap.put("tittle", cnGovernmentCloud.getServerName());
            resultList.add(cnGovernmentCloudMap);
        }

        // 帮助中心
        List<CnHelp> cnHelpList = cnHelpService.list(new LambdaQueryWrapper<CnHelp>()
                        .eq(CnHelp::getType, 2)
                        .like(CnHelp::getHelpName, keyword));
        for (CnHelp cnHelp : cnHelpList) {
            Map<String, String> cnHelpMap = new HashMap<>();
            cnHelpMap.put("type", "help");
            cnHelpMap.put("id", cnHelp.getHelpId());
            cnHelpMap.put("tittle", cnHelp.getHelpName());
            resultList.add(cnHelpMap);
        }


        // 解决方案
        List<CnSolution> cnSolutionList = cnSolutionService.list(new LambdaQueryWrapper<CnSolution>()
                        .isNotNull(CnSolution::getContent)
                        .like(CnSolution::getSolutionName, keyword));
        for (CnSolution cnSolution : cnSolutionList) {
            Map<String, String> cnSolutionMap = new HashMap<>();
            cnSolutionMap.put("type", "solution");
            cnSolutionMap.put("id", cnSolution.getParentId());
            cnSolutionMap.put("tittle", cnSolution.getSolutionName());
            resultList.add(cnSolutionMap);
        }


        return AjaxResult.success(resultList);
    }
}
